package com.fs.web.controller;

import com.auth0.jwt.JWTSigner;
import com.fs.common.model.R;
import com.fs.web.feign.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "测试接口",description="用于测试各种接口",tags = "/test")
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @Value("${jwt.issuer}")
    private String issuer;

    @Value("${jwt.secret}")
    private String secret;

    @Value("${jwt.exp}")
    private Long exp;


    @Autowired
    private AdminService adminService;

    @ApiOperation("测试")
    @GetMapping("/1")
    public R test(){
        R r=adminService.add();
        return  r;
    }


    @ApiOperation("用户登录")
    @GetMapping("/login")
    public String login(
            @ApiParam("账号")@RequestParam("account")String account,
            @ApiParam("密码")@RequestParam("password")String password){

        if (password.equals("123456")&&account.equals("admin")){
            //登陆成功
            //生成调用凭证
            long iat = System.currentTimeMillis() / 1000L; // issued at claim
            exp += iat; // expires claim. In this case the token expires in 60 seconds
            JWTSigner signer = new JWTSigner(secret);
            HashMap<String, Object> claims = new HashMap<String, Object>();
            claims.put("iss", issuer);
            claims.put("exp", exp);
            claims.put("iat", iat);
            claims.put("uid","1");//接受者userId
            claims.put("account","admin");//用户名称

            String jwt = signer.sign(claims);

            return R.ok(jwt).toString();
        }
        return R.error("用户名或密码错误").toString();

    }
}
